<?php
/**
 * Bootstrap abstract class
 *
 * @auth kim
 * @version 1.0
 *
 */

/**
 * Two short tags for use
 * DS => directory separator
 * PS => path separator
 *
 */
define('DS', DIRECTORY_SEPARATOR);
define('PS', PATH_SEPARATOR);

/**
 * We make sure we got the project root defined
 *
 */
if (!defined('PROJECT_ROOT')) {
    define('PROJECT_ROOT', dirname(dirname(__FILE__)));
}

/**
 * Bootstrap abstract class
 *
 * @auth kim
 * @version 1.0
 *
 */
abstract class Bootstrap_Abstract
{
    /**
     * Project environment locale setting
     * DEVELOPMENT  => development time
     * TESTING      => preproduction testing
     * PRODUCTION   => on production serve
     *
     */
    const DEVELOPMENT   = 'development';
    const TESTING       = 'testing';
    const PRODUCTION    = 'production';

    public static function setProjectEnvironment()
    {
        $fileEnv = PROJECT_ROOT . DS . 'Configuration' . DS . 'Environment.ini';

        if (!file_exists($fileEnv)) {
            //define('PROJECT_ENVIRONMENT', self::PRODUCTION);
            exit('Environment.ini missing !');
        } else {
            $env = @file_get_contents($fileEnv);

            switch ($env) {
                case self::DEVELOPMENT:
                case self::TESTING:
                    define('PROJECT_ENVIRONMENT', $env);
                    break;
                default:
                    define('PROJECT_ENVIRONMENT', self::PRODUCTION);
                    echo PROJECT_ENVIRONMENT . '!';
                    break;
            }
        }
    }

    /**
     * Error display
     * To show the error or not
     *
     */
    public static function setErrorDisplay()
    {
        if (!defined('PROJECT_ENVIRONMENT')) {
            self::setProjectEnvironment();
        }

        ini_set('error_reporting', E_ALL | E_STRICT);
        
        switch (PROJECT_ENVIRONMENT) {
            case self::DEVELOPMENT:
                ini_set('display_startup_errors', 1);
                ini_set('display_errors', 1);
                break;
            case self::TESTING:
                ini_set('display_startup_errors', 0);
                ini_set('display_errors', 1);
                break;
            default :
                ini_set('display_startup_errors', 0);
                ini_set('display_errors', 0);
                break;
        }
    }

    /**
     * The include paths
     *
     * Important !!
     * Please make sure you set the ZendFramework library within your project
     * even you have already set it in php.ini.
     * And make sure you put it in the first place of include paths !
     * Because this will straight affact the loading speed of Zend_Loader !!
     *
     */
    public static function setIncludePath()
    {
        $paths = '';

        if (!defined('PROJECT_ENVIRONMENT')) {
            self::setProjectEnvironment();
        }

        switch (PROJECT_ENVIRONMENT) {
            case self::DEVELOPMENT:
                $paths .= dirname(PROJECT_ROOT) . DS . 'ZendFramework' . DS . 'library' . PS;
                break;
            case self::TESTING:
                $paths .= dirname(PROJECT_ROOT) . DS . 'zend' . DS . 'library' . PS;
                break;
            default:
                $paths .= dirname(PROJECT_ROOT) . DS . 'ZendFramework' . DS . 'library' . PS;
                break;
        }

        $paths = $paths
               . PROJECT_ROOT . DS . 'Application' . DS . 'Zend' . PS
               . PROJECT_ROOT . DS . 'Configuration' . DS . 'Zend' . PS
               //. PROJECT_ROOT . DS . 'Error' . PS
               //. PROJECT_ROOT . DS . 'Library' . PS
               . get_include_path() . PS;

        set_include_path($paths);
    }

    /**
     * Boot your application
     * 1.  your project environment setting.
     * 2.  your errors display setting.
     * 3.  your files include path configuration.
     *
     */
    public static function boot()
    {
        self::setProjectEnvironment();

        self::setErrorDisplay();
        
        self::setIncludePath();
    }

    /**
     * We boot and we run !
     *
     */
    public function run()
    {
        self::boot();
    }


}